<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html lang="zh-CN">
<head> 
   <meta charset="UTF-8">
   <%@ include file="/WEB-INF/views/include/head.jsp"%>
   <link rel="stylesheet" href="${ctxStatic}/css/common_erp.css">
   <link rel="stylesheet" href="${ctxStatic}/css/layerSm.css">
   <script src="${ctxStatic}/js/jquery-1.9.1.min.js"></script>
   <script src="${ctxStatic}/js/layer.js"></script>
   <script src="${ctxStatic}/js/style.js"></script>
   <script src="${ctxStatic}/jquery-barcode/jquery-barcode.js" type="text/javascript"></script>
   <script type="text/javascript">
	$(document).ready(function(){
		$("#contractProcessID").select2();
		$("#fabricatingID").select2();
		$("#styleID").select2();
		$("#inventoryCategory").select2();
		$("#inventoryRequestBarCodeDiv").empty().barcode($("#stockBarCode").val(), "code128",{barWidth:2, barHeight:30,showHRI:false});
		if ("${puttingRequest.id!=null && puttingRequest.id!=''}") {
			fabricatingFun(1);
		}
	});
	function pad2(n) { return n < 10 ? '0' + n : n }
	
	// 返回年月日时分秒
	function generateTimeReqestNumber() {
	    var date = new Date();
	    return date.getFullYear().toString() + pad2(date.getMonth() + 1) + pad2(date.getDate()) + pad2(date.getHours()) + pad2(date.getMinutes()) + pad2(date.getSeconds());
	}
	function styleFun(num){
		
		var styleID = $("#styleID").find("option:selected").val();
    	var param={"styleID":styleID};
    	$.post("${ctx}/putting/request/styleFun",param,function(result){            
        	$("#contractProcessInfo").html($(callBackHtml(result)).find("#contractProcessInfo").html());
        	$("#tableRequestDetails").html($(callBackHtml(result)).find("#tableRequestDetails").html());
        	// 创建
        	if ("${puttingRequest.id==null || puttingRequest.id==''}") {
				var out = "OUT";
				var indentNo = $('#indentNoHtml').html();
				indentNo = out + indentNo;
				$("#inventoryRequestNo").val(indentNo);
			}
        },'html');
	}
	function fabricatingFun(num){
		var contractProcessID = "${puttingRequest.contractProcessID}";
		var fabricatingID = $("#fabricatingID").find("option:selected").val();
    	var params={"fabricatingID":fabricatingID,"contractProcessID":contractProcessID};
    	$.post("${ctx}/putting/request/fabricatingFun",params,function(result){            
        	$("#contractProcessHtml").html($(callBackHtml(result)).find("#contractProcessHtml").html());
        	$("#contractProcessID").select2();
        	if (num==1) {
        		contractProcessFun(num);
			}
    	},'html');
	}
	function contractProcessFun(num){
		var styleID = "${puttingRequest.styleID}";
		var contractProcessID = $("#contractProcessID").find("option:selected").val();
    	var param={"contractProcessID":contractProcessID,"styleID":styleID};
    	$.post("${ctx}/putting/request/contractProcessFun",param,function(result){            
        	$("#styleHtml").html($(callBackHtml(result)).find("#styleHtml").html());
        	$("#styleID").select2();
    	},'html');
	}
	function checkQuantity(t){
    	var inputQuantity = parseInt(($(t).val()).replace(/,/g,""));
    	var thisTR = t.parentNode.parentNode;
    	var materialQuantity = parseInt($(thisTR).find(".mQuantity").val());
    	if(materialQuantity<inputQuantity){
    		tipParent("<spring:message code="inventory_requestex_check_quantity_msg"/>");
    		$(t).val("");
    		return false;
    	}
    }
    //保存方法
    var saveForm={
         t:null,
         save:function(t){
              $(t).attr("disabled",true);
              this.t=t;
              // 表单验证
              $("#inputForm").validate({
              fail: function(element,e) {  
                  $(saveForm.t).attr("disabled",false);
              },
                   submitHandler: function() {
                	   $('#inventoryCategory').attr('disabled',false);
                   	   // 格式化，去除逗号
                       formUnFormat($("#inputForm"));
                        // 从表单获取数据信息
                        var params = $("#inputForm").serialize();
                        // 调用后台保存方法
                        $.post("${ctx}/putting/request/save",params,function(result){
                             // 提示信息
                             tipParent(result.message);
                             // 返回状态
                             if(result.statusCode==STATUSCODEOK){
                                 // 刷新页面
                                 window.parent.frames[0].searchListForm();
                                 closeWin();
                             }else{
                                  $(saveForm.t).attr("disabled",false);
                             }
                        },'json'); 
                   }
              });
              // 提交表单
              $("#inputForm").submit(); 
         } 
    }
    
	</script>
</head>
<body>
<div class="layerHeader clearfix">
	<c:choose>
	   <c:when test="${puttingRequest.id!=null && puttingRequest.id!=''}">
	    <!-- 编辑 --> 
			<p class="headerTitle"><spring:message code="inventory_requestex_create_editTitle"/></p>
       </c:when>
       <c:otherwise>
        <!-- 创建 --> 
            <p class="headerTitle"><spring:message code="inventory_requestex_create_createTitle"/></p>
       </c:otherwise>
    </c:choose>
    <div class="headerButton clearfix">
        <!-- 保存 -->
        <button type="button" onclick="saveForm.save(this)"><spring:message code="common_save"/></button>
        <!-- 取消 -->
        <button onclick="closeWin()"><spring:message code="btn_cancle"/></button>
    </div>
</div>
<form id="inputForm" method="post">
<c:set value="<%=ResourcesUtil.getConfig(\"outbound_category\")%>" var="varOutboundCategory" />
<input type="hidden" id="id" name="id" value="${puttingRequest.id}"/>
<input type="hidden" id="trIndex" name="trIndex" value="${puttingRequest.trIndex}"/>
<div class="smOrderBox">
    <div class="items clearfix">
        <div class="col-33 clearfix">
            <!-- 出库单号 -->
            <p class="tit"><spring:message code="inventory_requestex_create_requestNo"/></p>
            <p>${puttingRequest.inventoryRequestNo}</p>
            <input type="hidden" id="inventoryRequestNo" name="inventoryRequestNo" value="${puttingRequest.inventoryRequestNo}">
        </div>
        <div class="col-50 clearfix">
            <!-- 出库单条码 -->
            <p class="tit"><spring:message code="inventory_requestex_create_barCode"/></p>
            <div id="inventoryRequestBarCodeDiv"></div>
            <input type="hidden" id="stockBarCode" name="stockBarCode" value="${puttingRequest.stockBarCode}">
        </div>      
    </div>
    <div class="items clearfix">
        <div class="col-33 clearfix">
            <!-- 出库类别 -->
            <p class="tit"><spring:message code="inventory_requestex_create_inventoryCategory"/></p>
            <select style="width:150px" id="inventoryCategory" name="inventoryCategory" disabled="disabled">
            <option value=""><spring:message code="customer_pleaseChoose"/></option>
                <c:forEach items="${fns:getDictList(varOutboundCategory)}"   var="outboundCategoryInfo">
                    <option <c:if test="${fn:split(puttingRequest.inventoryCategory,'-')[1] eq outboundCategoryInfo.itemValue}">selected="selected"</c:if> value="${outboundCategoryInfo.itemCode}-${outboundCategoryInfo.itemValue}" > 
                        ${outboundCategoryInfo.itemName}
                    </option>
                </c:forEach>
            </select>
        </div>
        <div class="col-33 clearfix haveToBox">
            <!-- 加工厂名称 -->
            <p class="tit"><spring:message code="inventory_requestex_create_fabricatingName"/></p>
            <select style="width:150px" id="fabricatingID" onchange="fabricatingFun(0);" data-rule-required="true"  data-msg-required="<spring:message code="contract_error_msg_customerName"/>">
               <option value=""><spring:message code="customer_pleaseChoose"/></option>
               <c:forEach items="${puttingRequest.fabricatingList}" var="fabricating">
                  <option <c:if test="${puttingRequest.fabricatingID eq fabricating.fabricatingID}">selected="selected"</c:if> value="${fabricating.fabricatingID}" > 
                     ${fabricating.fabricatingName}
                  </option>
               </c:forEach>
            </select>
            <p class="haveTo">*</p>
        </div>
        <div id="contractProcessHtml" class="col-33 clearfix haveToBox">
            <!-- 外发合同 -->
            <p class="tit"><spring:message code="inventory_requestex_create_contractProcessNo"/></p>
            <select style="width:150px" id="contractProcessID" name="contractProcessID" onchange="contractProcessFun(0);" data-rule-required="true"  data-msg-required="<spring:message code="contract_error_msg_customerName"/>">
               <option value=""><spring:message code="customer_pleaseChoose"/></option>
               <c:forEach items="${puttingRequest.contractProcessList}" var="contractProcess">
                  <option <c:if test="${puttingRequest.contractProcessID eq contractProcess.contractProcessID}">selected="selected"</c:if> value="${contractProcess.contractProcessID}" > 
                     ${contractProcess.contractProcessNo}
                  </option>
               </c:forEach>
            </select>
            <p class="haveTo">*</p>
        </div>
    </div>
    <div class="items clearfix">
    <div id="contractProcessInfo" class="col-66">
        <div class="col-50 clearfix">
            <!-- 客户名称-->
            <p class="tit"><spring:message code="inventory_requestex_create_customerName"/></p>
            <p>${puttingRequest.venderName}</p>
        </div>
        <div class="col-50 clearfix">
            <!-- 客户订单号 -->
            <p class="tit" style="width: 75px;"><spring:message code="inventory_requestex_create_indentNo"/></p>
            <p id="indentNoHtml">${puttingRequest.indentNo}</p>
        </div>
     </div>
        <div id="styleHtml" class="col-33 clearfix haveToBox">
            <!-- 款号/名称 -->
            <p class="tit"><spring:message code="inventory_requestex_create_styleNoName"/></p>
            <select style="width:150px" id="styleID" name="styleID" onchange="styleFun(0);" data-rule-required="true"  data-msg-required="<spring:message code="contract_error_msg_customerName"/>">
               <option value=""><spring:message code="customer_pleaseChoose"/></option>
               <c:forEach items="${puttingRequest.styleList}" var="style">
                  <option <c:if test="${puttingRequest.styleID eq style.styleID}">selected="selected"</c:if> value="${style.styleID}" > 
                  	${style.styleCustomerNo}<br>${style.styleName}
                  </option>
               </c:forEach>
            </select>
            <p class="haveTo">*</p>
        </div>
    </div>
    <div class="items clearfix">
    	<div class="col-33 clearfix">
            <!-- 申请人 -->
            <p class="tit"><spring:message code="inventory_requestex_create_requestUser"/></p>
            <p>${puttingRequest.requestUser}</p>
        </div>
        <div class="col-33 clearfix">
            <!-- 申请日期 -->
            <p class="tit"><spring:message code="inventory_requestex_create_requestDate"/></p>
            <p><fmt:formatDate value="${puttingRequest.requestDate}" pattern="yyyy-MM-dd" /></p>
        </div>
    </div>
</div>
<div class="tableArea">
    <div class="tableScrollSm" style="overflow:auto;width:833px;height:360px;">
        <table cellpadding="0" class="tableHead">
            <thead>
            <tr>
            	<!-- 操作 -->
                <th style="width:50px;"><spring:message code="inventory_requestex_create_details_operate"/></th>
                <!-- 物料图片 -->
                <th class="tab-wltp"><spring:message code="inventory_requestex_create_details_img"/></th>
                <!-- 名称编码 -->
                <th><p class="tab-wlmcbj"><spring:message code="inventory_requestex_create_details_nameCode"/></p></th>
                <!-- 物料类别 -->
                <th><p class="width-80px"><spring:message code="inventory_requestex_create_details_materialCategory"/></p></th>
                <!-- 颜色 -->
                <th><p class="width-80px"><spring:message code="inventory_requestex_create_details_color"/></p></th>
                <!-- 成分 -->
                <th><p class="width-80px"><spring:message code="inventory_requestex_create_details_composition"/></p></th>
                <!-- 幅宽 -->
                <th><p class="width-80px"><spring:message code="inventory_requestex_create_details_spec"/></p></th>
                <!-- 库存数量 -->
                <th class="width-120px"><spring:message code="inventory_requestex_create_details_materialQuantity"/></th>
                <!-- 申请出库数量 -->
                <th class="width-120px"><spring:message code="inventory_requestex_create_details_requestQuantity"/></th>
            </tr>
            </thead>
        </table>
        <table cellpadding="0" class="tableBody" id="tableRequestDetails">
        <c:if test="${not empty puttingRequest.requestDetailsList}">
        	<tbody>
            <c:forEach items="${puttingRequest.requestDetailsList}" var="requestDetailsInfo" varStatus="status">
            <tr>
                <td style="width:50px;"></td>
                <td class="tab-wltp">
                  <c:if test="${not empty requestDetailsInfo.filePath}">
                        <img src="${requestDetailsInfo.filePath}" class="wltp">
                  </c:if>
                  <c:if test="${empty requestDetailsInfo.filePath}">
                        <img src="${ctxStatic}/images/pic-none.jpg" class="wltp">
                  </c:if>
                </td>
                <td><p class="tab-wlmcbj">${requestDetailsInfo.materialNameCn}<br>${requestDetailsInfo.materialCode}</p></td>
                <td><p class="width-80px">${fns:getDictName(requestDetailsInfo.materialCategory)}</p></td>
                <td><p class="width-80px">${requestDetailsInfo.materialColor}</p></td>
                <td><p class="width-80px">${requestDetailsInfo.materialComposition}</p></td>
                <td><p class="width-80px">${requestDetailsInfo.materialSpec}</p></td>
                <td class="subDetails width-120px"><fmt:formatNumber value="${requestDetailsInfo.materialQuantity}" pattern="#,##0.###"/></td>
                <td class="subDetails width-120px realQuantity"><input type="tel" name="requestDetailsList[${status.index}].requestQuantity" value="<fmt:formatNumber value="${requestDetailsInfo.requestQuantity}" pattern="#,##0.###"/>" maxlength="11" onblur="formatNumber(this);checkQuantity(this);" onfocus="unFormat(this)"></td>
                <input type="hidden" name="requestDetailsList[${status.index}].materialId" value="${requestDetailsInfo.materialId}"/>
                <input type="hidden" class="mQuantity" value="${requestDetailsInfo.materialQuantity}"/>
            </tr>
            </c:forEach>
            </tbody>
            </c:if>
        </table>
    </div>
</div>
</form>
</body>
</html>